package com.all.play.myapplication.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.all.play.myapplication.Bean.GoodsInfo;
import com.all.play.myapplication.R;
import com.all.play.myapplication.activity.ProductDetialActivity;
import com.all.play.myapplication.utils.CartSorage;
import com.all.play.myapplication.view.NumberSubAddView;
import com.all.play.myapplication.view.PducShowDialog;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by 聪明一只哈 on 2018/4/19.
 * Time: 2018/4/19  11:01
 */

public class MyPdutAdapter extends RecyclerView.Adapter<MyPdutAdapter.MyPducViewHolder> {
    private Context context;
    private List<GoodsInfo> list;
    private PducShowDialog showDialog;

    public MyPdutAdapter(Context context, List<GoodsInfo> list) {

        this.context = context;
        this.list = list;
    }

    @Override
    public MyPducViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_rv_pduc_manager, null);
        return new MyPducViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyPducViewHolder holder, final int position) {

        final GoodsInfo goodsInfo = list.get(position);
        if (goodsInfo != null) {

            Glide.with(context).load(goodsInfo.getFigure()).into(holder.image_pdut);

            holder.setData(position, goodsInfo);
            holder.name.setText(goodsInfo.getName());
            holder.time.setText(goodsInfo.getCover_price() + "¥");

            holder.numberAddSubView.setValue(goodsInfo.getNumber());

            //设置数量增加和减少的监听
            holder.numberAddSubView.setOnNumberChangeListener(new NumberSubAddView.OntvNumberChangeListener() {
                @Override
                public void onChange(int value) {
                    //1.value,改变,则内存中的数据需要改变
                    goodsInfo.setNumber(value);
                    //2.内存缓存中数据更新,保存本地
                    CartSorage.getInstance().updateData(goodsInfo);
                    //3.刷新当前条目
                    notifyItemChanged(position);


                }
            });

            //删除数据
            holder.tv_item_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "删除数据", Toast.LENGTH_SHORT).show();
                    //内存移除
                    list.remove(goodsInfo);
                    //本地保存
                    CartSorage.getInstance().deleteData(goodsInfo);
                    //adapter刷新
                    notifyDataSetChanged();

                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    class MyPducViewHolder extends RecyclerView.ViewHolder {

        ImageView image_pdut;
        TextView name, time, tv_item_delete;
        NumberSubAddView numberAddSubView;

        public MyPducViewHolder(View itemView) {
            super(itemView);
            image_pdut = (ImageView) itemView.findViewById(R.id.image_pdut);
            name = (TextView) itemView.findViewById(R.id.tv_pdut_name);
            time = (TextView) itemView.findViewById(R.id.tv_pdut_time);
            tv_item_delete = (TextView) itemView.findViewById(R.id.tv_item_delete);
            numberAddSubView = (NumberSubAddView) itemView.findViewById(R.id.numberAddSubView);


        }


        public void setData(int position, final GoodsInfo goodsInfo) {

            itemView.setTag(position);

            //点击跳转
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = (int) view.getTag();
                    Intent intent = new Intent(context, ProductDetialActivity.class);
                    intent.putExtra("pdut_name", goodsInfo.getName());
                    intent.putExtra("pdut_number", goodsInfo.getNumber()+"");
                    Log.i("ProductDetial", "onCreate:"+goodsInfo.getNumber());
                    intent.putExtra("pdut_price", goodsInfo.getCover_price());
                    intent.putExtra("pdut_figuer", goodsInfo.getFigure());
                    context.startActivity(intent);

                }
            });


            //长按修改内容
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    Toast.makeText(context, "长按修改内容!", Toast.LENGTH_SHORT).show();
                    final int position = (int) view.getTag();
//                    Log.i("onLongClick", "onLongClick: " + goodsInfo.toString());
                    String name = goodsInfo.getName();
                    String cover_price = goodsInfo.getCover_price();
                    showDialog = new PducShowDialog((Activity) context,
                            R.style.show_shop_message_dialog,
                            new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    switch (view.getId()) {
                                        case R.id.bt_change:
                                            //获取更改后的数据
                                            String change_name = showDialog.pduc_name.getText().toString();
                                            String change_price = showDialog.pdut_price.getText().toString();
                                            Log.i("onLongClick", "onLongClick: " + change_name+"~~~"+change_price);
                                            goodsInfo.setName(change_name);
                                            goodsInfo.setCover_price(change_price);
                                            //保存到本地
                                            CartSorage.getInstance().updateData(goodsInfo);
                                            //条目数据刷新
                                            notifyItemChanged(position);
                                            //notifyDataSetChanged();
                                            showDialog.dismiss();
                                            break;
                                    }
                                }
                            },
                            name, cover_price);


                    showDialog.show();

                    return true;
                }
            });


        }
    }
}
